package com.leps.nq.tx.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.tx.entity.Section;
import com.leps.nq.tx.entity.SectionPreset;
import com.leps.nq.tx.service.ISectionPresetService;
import com.leps.nq.tx.service.ISectionService;
import com.leps.nq.tx.vo.CreateSectionVo;
import com.leps.nq.tx.vo.SectionQueryModel;
import com.leps.nq.tx.vo.SectionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 点位区段表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-07-23
 */
@RestController
@RequestMapping("/section")
@Api(tags = "点位区段管理")
public class SectionController {
    @Autowired
    ISectionService sectionService;
    @Autowired
    ISectionPresetService presetService;

    @ApiOperation("添加区段")
    @PostMapping
    @Transactional
    @Log(title = "点位区段", businessType = BusinessType.INSERT)
    public R create(@RequestBody CreateSectionVo vo) {
        // clear is_default if this section is new default of plctype1 category
        if (vo.getIsDefault()) {
            this.sectionService.lambdaUpdate()
                    .set(Section::getIsDefault, false)
                    .eq(Section::getPlcType1, vo.getPlcType1())
                    .update();
        }
        // save section
        Section section = BeanUtil.toBean(vo, Section.class);
        this.sectionService.save(section);
        // save presets
        List<SectionPreset> presets = vo.getPresets();
        if(ObjectUtil.isNotEmpty(presets)){
            presets.forEach(p -> p.setSectionId(section.getSectionId()));
            this.presetService.saveBatch(presets);
        }
        return RBuilder.ok();
    }

    @ApiOperation("查询所有区段及点位预设")
    @GetMapping
    public R<List<SectionVo>> list() {
        List<SectionVo> list = this.sectionService.listVoByQueryModel(new SectionQueryModel());
        return RBuilder.ok(list);
    }

    @ApiOperation("根据PLC大类别查询区段及点位预设")
    @GetMapping("/listByPlcType1")
    public R<List<SectionVo>> listByPlcType1(String plcType1) {
        SectionQueryModel queryModel = new SectionQueryModel();
        queryModel.setPlcType1(plcType1);
        List<SectionVo> list = this.sectionService.listVoByQueryModel(queryModel);
        return RBuilder.ok(list);
    }

    @ApiOperation("根据id查询区段及点位预设详细信息")
    @GetMapping("/{id}")
    public R<SectionVo> getById(@PathVariable int id) {
        SectionQueryModel queryModel = new SectionQueryModel();
        queryModel.setSectionId(id);
        List<SectionVo> list = this.sectionService.listVoByQueryModel(queryModel);
        SectionVo sectionVo = list.stream().findFirst().orElse(null);
        return RBuilder.ok(sectionVo);
    }

    @ApiOperation("修改区段及点位预设")
    @PutMapping
    @Transactional
    @Log(title = "点位区段", businessType = BusinessType.UPDATE)
    public R update(@RequestBody SectionVo vo) {
        Section section = BeanUtil.toBean(vo, Section.class);
        this.sectionService.updateById(section);
        presetService.remove(new LambdaQueryWrapper<SectionPreset>().eq(SectionPreset::getSectionId, section.getSectionId()));
        List<SectionPreset> presets = vo.getPresets();
        if(ObjectUtil.isNotEmpty(presets)) {
            presets.forEach(p -> p.setSectionId(section.getSectionId()));
            this.presetService.saveBatch(presets);
        }
        return RBuilder.ok();
    }

    @ApiOperation("删除区段")
    @DeleteMapping
    @Log(title = "点位区段", businessType = BusinessType.DELETE)
    public R remove(@RequestBody List<Integer> sectionIds) {
        this.sectionService.removeBatchByIds(sectionIds);
        return RBuilder.ok();
    }
}
